package cn.alm.serviceorder.controller;

import cn.alm.serviceorder.dto.CreateOrderDto;
import cn.alm.serviceorder.dto.WeatherDto;
import cn.alm.serviceorder.entity.Order;
import cn.alm.serviceorder.feign.WeatherFeignClient;
import cn.alm.serviceorder.service.OrderService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private WeatherFeignClient weatherFeignClient;

//    @Value("${order.timeout}")
//    private String orderTimeout;
//
//    @Value("${order.auto-confirm}")
//    private String orderAutoConfirm;

    // 创建订单
    @PostMapping("create")
    @SentinelResource
    public Order createOrder(@RequestBody CreateOrderDto createOrderDto) {
        System.out.println("api order create enter");
        Order order = orderService.createOrder(createOrderDto.getProductId(), createOrderDto.getUserId());
        return order;
    }

//    @GetMapping("config")
//    public String getConfig() {
//        return "timeout: " + orderTimeout + ", auto-confirm: " + orderAutoConfirm;
//    }

    @PostMapping("weather")
    public String getWeather(@RequestHeader("x-token") String token, @RequestBody WeatherDto dto) {
        String weather = weatherFeignClient.getWeather(dto, token);
        return weather;
    }

    @GetMapping("write_db")
    public String writeDb() {
        return "write db successfully...";
    }

    @GetMapping("read_db")
    public String readDb() {
        return "read db successfully...";
    }

    @GetMapping("test_low_request")
    public String testLowRequest() {
        return weatherFeignClient.testLowRequest();
    }

    @GetMapping("req_ex")
    public String reqEx() {
        return weatherFeignClient.reqEx();
    }

}
